//
//  RegisterViewController.m
//  大秀
//
//  Created by admin on 16/4/22.
//  Copyright © 2016年 yunzhen. All rights reserved.
//


//注册登录接口
//
//发送
//
//http://172.16.52.24:8888/bigtobig/sms/send_random_message?mobile=
//
//获取
//
//http://172.16.52.24:8888/bigtobig/sms/get_random_message?mobile=
#import "RegisterViewController.h"
#import "HomeController.h"
#import "JKCountDownButton.h"
#import "registerModel.h"
@interface RegisterViewController ()<UIScrollViewDelegate,UITextFieldDelegate>

@property (weak ,nonatomic)UIScrollView *scrollView;
@property (strong ,nonatomic)UIPageControl *pageControl;
@property (weak ,nonatomic)UITextField *inputPhoneNubText;
@property (weak ,nonatomic)UITextField *inputSecurityCodeText;
@property (weak ,nonatomic)UITextField *inputSecreatText;
@property (weak ,nonatomic)UITextField *sureSecreatText;
@property (weak ,nonatomic)UITextField *inputNameNubText;
@property (weak ,nonatomic)UIButton *agreeCircleBtn;
@property (weak ,nonatomic)JKCountDownButton *getSecurityCodeBtn;
@property (strong ,nonatomic)NSMutableArray *registerArray;
@property (strong ,nonatomic)registerModel *registerModel;
@end

@implementation RegisterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self makeUI];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
- (void)makeUI{

    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.titleView.tintColor = [UIColor whiteColor];
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    title.textAlignment = NSTextAlignmentCenter;
    title.text = @"快速注册";
    title.textColor = [UIColor grayColor];
    self.navigationItem.titleView = title;
    //自动调整scrollview的 inset,设置为no，不让viewController调整比如两个tableview从第一个push到第二个第一个设置frame(0,0,w,h)没问题push到第二个就被导航栏遮住部分cell必须设置成（0，64，w,h）都是因为automaticallyAdjustsScrollViewInsets的原因设置成no就好了所有这里是为了防止scrollview滑动回来的时候被导航栏遮住一块
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    
    UIPageControl* pageControl = [[UIPageControl alloc] init];
    pageControl.numberOfPages = 3;
    self.pageControl = pageControl;
    
    
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64)];
    //隐藏水平方向的滚动条
    scrollView.userInteractionEnabled = YES;
    scrollView.showsHorizontalScrollIndicator = NO;
    //开启分页
    scrollView.pagingEnabled = YES;
    //监听滑动-->成为代理
    scrollView.delegate = self;
    scrollView.contentSize = CGSizeMake(scrollView.bounds.size.width * 3, 0);
    scrollView.backgroundColor = [UIColor colorWithRed:240/255.0 green:239/255.0 blue:237/255.0 alpha:1];
    scrollView.bounces = NO;
    scrollView.scrollEnabled = NO;
    self.scrollView = scrollView;

    [self.view addSubview:scrollView];
    [self addReginsterView:scrollView];
  
}

- (void)addReginsterView:(UIScrollView *)reginsterView{
    

    for (int y = 0; y < 3; y++) {
        
            if (y == 0) {

                
                UIButton *nextBtn = [[UIButton alloc]initWithFrame:CGRectMake(10 + y*kScreenWidth, 166, kScreenWidth - 20, 45)];
                nextBtn.backgroundColor = [UIColor colorWithRed:92/255.0 green:167/255.0 blue:252/255.0 alpha:1];
                [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
                [nextBtn addTarget:self action:@selector(next) forControlEvents:UIControlEventTouchUpInside];
                nextBtn.layer.cornerRadius = 5.0f;
                [nextBtn.layer setMasksToBounds:YES];
                [reginsterView addSubview:nextBtn];
                

    for (int i = 0; i < 3; i++) {
        
        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(y * kScreenWidth,i *55, kScreenWidth, 45)];
        view.backgroundColor = [UIColor whiteColor];
        [reginsterView addSubview:view];
    
        if (i == 0) {
    
            for (int i = 0 ; i < 3; i++) {
        
        UITextField *inputText = [[UITextField alloc]initWithFrame:CGRectMake(0 + kScreenWidth/3*i, 0, kScreenWidth/3, 40)];
        inputText.textColor = [UIColor grayColor];
        inputText.font = [UIFont systemFontOfSize:13.0];
        inputText.textAlignment = NSTextAlignmentCenter;
        inputText.userInteractionEnabled = NO;
        [view addSubview:inputText];
        
        
        if (i == 0) {

            inputText.textColor = [UIColor colorWithRed:92/255.0 green:167/255.0 blue:252/255.0 alpha:1];
            inputText.text = @"1.输入手机号  >";

        }
        if (i == 1) {
            
            inputText.text = @"2.输入密码  >";
            
        }
        if (i == 2) {
            
            inputText.text = @"3.输入姓名  >";
            
        }
        
    }

    }
    if (i == 1) {
        
        UITextField *inputPhoneNubText = [[UITextField alloc]initWithFrame:CGRectMake(10, 0, kScreenWidth - 150, 45)];
        inputPhoneNubText.textAlignment = NSTextAlignmentLeft;
        inputPhoneNubText.textColor = [UIColor grayColor];
        inputPhoneNubText.placeholder = @"请输入手机号";
//        inputPhoneNubText.text =
        inputPhoneNubText.delegate = self;
        inputPhoneNubText.clearButtonMode = UITextFieldViewModeAlways;
        inputPhoneNubText.keyboardType = UIKeyboardTypeNumberPad;
        self.inputPhoneNubText = inputPhoneNubText;
        
        
        JKCountDownButton *getSecurityCodeBtn = [[JKCountDownButton alloc]initWithFrame:CGRectMake(kScreenWidth - 100, 7.5, 80, 30)];
        [getSecurityCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
        [getSecurityCodeBtn setTitleColor:[UIColor colorWithRed:92/255.0 green:167/255.0 blue:252/255.0 alpha:1] forState:UIControlStateNormal];
        getSecurityCodeBtn.titleLabel.font = [UIFont systemFontOfSize:12.0];
        self.getSecurityCodeBtn = getSecurityCodeBtn;
        getSecurityCodeBtn.layer.cornerRadius = 2.0f;
        [getSecurityCodeBtn.layer setMasksToBounds:YES];
        [getSecurityCodeBtn addTarget:self action:@selector(getSecurityCode) forControlEvents:UIControlEventTouchUpInside];
        [getSecurityCodeBtn.layer setBorderColor:[UIColor colorWithRed:92/255.0 green:167/255.0 blue:252/255.0 alpha:1].CGColor];
        [getSecurityCodeBtn.layer setBorderWidth:1];
        getSecurityCodeBtn.backgroundColor = [UIColor whiteColor];

        [view addSubview:getSecurityCodeBtn];
        [view addSubview:inputPhoneNubText];
        
        }
    if (i == 2) {
            
        UITextField *inputSecurityCodeText = [[UITextField alloc]initWithFrame:CGRectMake(10, 0, kScreenWidth - 150, 45)];
        inputSecurityCodeText.textAlignment = NSTextAlignmentLeft;
        inputSecurityCodeText.delegate = self;
        inputSecurityCodeText.textColor = [UIColor grayColor];
        inputSecurityCodeText.placeholder = @"请输入验证码";
        self.inputSecurityCodeText = inputSecurityCodeText;
        self.inputSecurityCodeText.keyboardType = UIKeyboardTypeNumberPad;
        [view addSubview:inputSecurityCodeText];
            
            
        }
        
     
    }
                
            }
    else if(y == 1) {
        
        UIButton *nextBtn = [[UIButton alloc]initWithFrame:CGRectMake(10 + y*kScreenWidth, 166, kScreenWidth - 20, 45)];
        nextBtn.backgroundColor = [UIColor colorWithRed:92/255.0 green:167/255.0 blue:252/255.0 alpha:1];
        [nextBtn setTitle:@"下一步" forState:UIControlStateNormal];
        [nextBtn addTarget:self action:@selector(nextTwo) forControlEvents:UIControlEventTouchUpInside];
        nextBtn.layer.cornerRadius = 5.0f;
        [nextBtn.layer setMasksToBounds:YES];
        [reginsterView addSubview:nextBtn];


//        
        for (int i = 0; i < 3; i++) {

        
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(y * kScreenWidth,i *55, kScreenWidth, 45)];
        view.backgroundColor = [UIColor whiteColor];
        [reginsterView addSubview:view];
        
        
    if (i == 0) {
        
        for (int i = 0 ; i < 3; i++) {
            
            UITextField *inputText = [[UITextField alloc]initWithFrame:CGRectMake(0 + kScreenWidth/3*i, 0, kScreenWidth/3, 40)];
            inputText.textColor = [UIColor grayColor];
            inputText.font = [UIFont systemFontOfSize:13.0];
            inputText.textAlignment = NSTextAlignmentCenter;
            inputText.userInteractionEnabled = NO;
            [view addSubview:inputText];
            
            
            if (i == 0) {
                
     
                inputText.text = @"1.输入手机号  >";
                
            }
            if (i == 1) {
                
                inputText.textColor = [UIColor colorWithRed:92/255.0 green:167/255.0 blue:252/255.0 alpha:1];
                inputText.text = @"2.输入密码  >";
                
            }
            if (i == 2) {
                
                inputText.text = @"3.输入姓名  >";
                
            }
            
        }
        
    }
    if (i == 1) {
        
        UITextField *inputSecreatNubText = [[UITextField alloc]initWithFrame:CGRectMake(10, 0, kScreenWidth - 30, 45)];
        inputSecreatNubText.textAlignment = NSTextAlignmentLeft;
        inputSecreatNubText.textColor = [UIColor grayColor];
        inputSecreatNubText.placeholder = @"请输入密码";
        inputSecreatNubText.delegate = self;
        inputSecreatNubText.clearButtonMode = UITextFieldViewModeAlways;
        inputSecreatNubText.secureTextEntry = YES;
        self.inputSecreatText = inputSecreatNubText;
        [view addSubview:inputSecreatNubText];
        
    }
    if (i == 2) {
        
        UITextField *sureSecreatNubText = [[UITextField alloc]initWithFrame:CGRectMake(10, 0, kScreenWidth - 30, 45)];
        sureSecreatNubText.textAlignment = NSTextAlignmentLeft;
        sureSecreatNubText.textColor = [UIColor grayColor];
        sureSecreatNubText.placeholder = @"确定密码";
        sureSecreatNubText.clearButtonMode = UITextFieldViewModeAlways;
        sureSecreatNubText.secureTextEntry = YES;
        self.sureSecreatText = sureSecreatNubText;
        sureSecreatNubText.delegate = self;
        [view addSubview:sureSecreatNubText];
        
        
    }
            
        }
        
        }else{
            
            UIButton *nextBtn = [[UIButton alloc]initWithFrame:CGRectMake(10 + y*kScreenWidth, 110, kScreenWidth - 20, 45)];
            nextBtn.backgroundColor = [UIColor colorWithRed:92/255.0 green:167/255.0 blue:252/255.0 alpha:1];
            [nextBtn setTitle:@"完成" forState:UIControlStateNormal];
            [nextBtn addTarget:self action:@selector(complate) forControlEvents:UIControlEventTouchUpInside];
            nextBtn.layer.cornerRadius = 5.0f;
            [nextBtn.layer setMasksToBounds:YES];
            [reginsterView addSubview:nextBtn];

                UIButton *agreeCircleBtn = [[UIButton alloc]initWithFrame:CGRectMake(10 + y*kScreenWidth, 181, 15.5, 15.5)];
                [agreeCircleBtn setImage:[UIImage imageNamed:@"未选中_08"] forState:UIControlStateNormal];
                [agreeCircleBtn setImage:[UIImage imageNamed:@"选中_08"] forState:UIControlStateSelected];
                [agreeCircleBtn addTarget:self action:@selector(agreeCircleClick:) forControlEvents:UIControlEventTouchUpInside];
            agreeCircleBtn.selected = YES;
                self.agreeCircleBtn = agreeCircleBtn;
                [reginsterView addSubview:agreeCircleBtn];
            
            
                UILabel *agreeLb = [[UILabel alloc]initWithFrame:CGRectMake(30 + y*kScreenWidth, 165, 80, 45)];
                agreeLb.text = @"我阅读并同意";
                agreeLb.font = [UIFont systemFontOfSize:13.0];
                agreeLb.textColor = [UIColor grayColor];
                [reginsterView addSubview:agreeLb];
            
            
                UIButton *agreeBtn = [[UIButton alloc]initWithFrame:CGRectMake(90 + y*kScreenWidth, 165, 200, 45)];
                agreeBtn.titleLabel.font = [UIFont systemFontOfSize:13.0];
                [agreeBtn setTitle:@"《 大秀创投用户服务协议 》" forState:UIControlStateNormal];
                [agreeBtn addTarget:self action:@selector(agreeDeal) forControlEvents:UIControlEventTouchUpInside];
                [agreeBtn setTitleColor:[UIColor colorWithRed:92/255.0 green:167/255.0 blue:252/255.0 alpha:1] forState:UIControlStateNormal];
                [reginsterView addSubview:agreeBtn];
        
        for (int i = 0; i < 3; i++) {


        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(y * kScreenWidth,i *55, kScreenWidth, 45)];
        view.backgroundColor = [UIColor whiteColor];
        [reginsterView addSubview:view];
        
        if (i == 0) {
            
            for (int i = 0 ; i < 3; i++) {
                
                UITextField *inputText = [[UITextField alloc]initWithFrame:CGRectMake(0 + kScreenWidth/3*i, 0, kScreenWidth/3, 40)];
                inputText.textColor = [UIColor grayColor];
                inputText.font = [UIFont systemFontOfSize:13.0];
                inputText.textAlignment = NSTextAlignmentCenter;
                inputText.userInteractionEnabled = NO;
                [view addSubview:inputText];
                
                
                if (i == 0) {
                    
                    
                    inputText.text = @"1.输入手机号  >";
                    
                }
                if (i == 1) {
                    
       
                    inputText.text = @"2.输入密码  >";
                    
                }
                if (i == 2) {
                    
                    inputText.textColor = [UIColor colorWithRed:92/255.0 green:167/255.0 blue:252/255.0 alpha:1];
                    inputText.text = @"3.输入姓名  >";
                    
                }
                
            }
            
        }
        if (i == 1) {
            
            UITextField *inputNameNubText = [[UITextField alloc]initWithFrame:CGRectMake(10, 0, kScreenWidth - 150, 45)];
            inputNameNubText.textAlignment = NSTextAlignmentLeft;
            inputNameNubText.textColor = [UIColor grayColor];
            inputNameNubText.placeholder = @"请输入姓名";
            self.inputNameNubText = inputNameNubText;
            [view addSubview:inputNameNubText];
            
        }
        if (i == 2) {

            [view removeFromSuperview];
        }
        
        
    }
        
    }
        

}
    
}

- (void)agreeCircleClick:(UIButton *)agreeCircleBtn{

    agreeCircleBtn.selected = !agreeCircleBtn.selected;
    
}
- (void)getSecurityCode{


    if(self.inputPhoneNubText.text.length == 0){
    
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"请填写手机号码";
        [hud hide:YES afterDelay:1];
    }
        else if (![ZYUtils validateMobile:self.inputPhoneNubText.text]){

        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"手机格式不正确";
        [hud hide:YES afterDelay:1];
    }
        else{

            
            Reachability *reach = [Reachability reachabilityForInternetConnection];
            NetworkStatus status = [reach currentReachabilityStatus];
            if (status == NotReachable) {
                
                MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
                HUD.labelText = @"请检查网络连接";
                HUD.mode = MBProgressHUDModeText;
                [HUD hide:YES afterDelay:1.0];
                
                return;
                
            }

        [self yanzhengma];
        
        NSDictionary *params = @{@"mobile":self.inputPhoneNubText.text};
        [HttpTool postWithURL:@"http://61.135.176.43:6080/bigtobig/sms/send_random_message?" params:params success:^(id json) {
            NSLog(@"%@",json);
            
            NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
            
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableLeaves error:nil];
            NSLog(@"%@",dict);

            
        } failure:^(NSError *error) {
            
            if (error.code == -1004) {
                
                MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
                HUD.labelText = @"网络请求失败";
                HUD.mode = MBProgressHUDModeText;
                [HUD hide:YES afterDelay:1.0];
                
            }

        }];
    
    }
}

- (void)agreeDeal{
    
    UIViewController *webVc=[[UIViewController alloc]init];
    
    UIWebView *webV=[[UIWebView alloc]initWithFrame:webVc.view.bounds];
    NSURL *url = [NSURL URLWithString:@"http://www.bigtobig.com"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    [webV loadRequest:request];
    
    [webVc.view addSubview:webV];
    
    [self.navigationController pushViewController:webVc animated:YES];

}
-(void)yanzhengma{
    
    __block int timeout=30; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self.getSecurityCodeBtn setTitle:@"获取验证码" forState:UIControlStateNormal];
                self.getSecurityCodeBtn.userInteractionEnabled = YES;
//                self.getSecurityCodeBtn.backgroundColor = COLOR_RGBA(253, 82, 75, 1);
                self.getSecurityCodeBtn.userInteractionEnabled = YES;
            });
        }else{
            //            int minutes = timeout / 60;
            int seconds = timeout % 60;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                // NSLog(@"____%@",strTime);
                [self.getSecurityCodeBtn setTitle:[NSString stringWithFormat:@"%@秒重新发送",strTime] forState:UIControlStateNormal];
//                self.getSecurityCodeBtn.backgroundColor = [UIColor grayColor];
                self.getSecurityCodeBtn.userInteractionEnabled = NO;
                
            });
            timeout--;
            
        }
    });
    dispatch_resume(_timer);
    
}
- (void)nextTwo{

    
    
    if (self.inputSecreatText.text.length <6||self.inputSecreatText.text.length > 18) {
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"密码长度需大于5位小于17位";
        [hud hide:YES afterDelay:1];
        
        
        
    }else if(![self.inputSecreatText.text isEqualToString: self.sureSecreatText.text]) {
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"两次密码输入不一致";
        [hud hide:YES afterDelay:1];
        

    }else{
        
    [self.scrollView setContentOffset:CGPointMake(2 *kScreenWidth, 0) animated:YES];
        
    }
}
- (void)next{

    
    if (self.inputPhoneNubText.text.length == 0) {
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"请输入手机号";
        [hud hide:YES afterDelay:1];
        
        
    }else if (![ZYUtils validateMobile:self.inputPhoneNubText.text]){
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"手机号格式不正确";
        [hud hide:YES afterDelay:1];
    }else if (self.inputSecurityCodeText.text.length == 0){
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"请输入验证码";
        [hud hide:YES afterDelay:1];
        
    }else if(self.inputSecurityCodeText.text.length !=4){
        
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"验证码为四位数字";
        [hud hide:YES afterDelay:1];
        
    }else{
        
        Reachability *reach = [Reachability reachabilityForInternetConnection];
        NetworkStatus status = [reach currentReachabilityStatus];
        
        if (status == NotReachable) {
            
            MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
            HUD.labelText = @"请检查网络连接";
            HUD.mode = MBProgressHUDModeIndeterminate;
            [HUD hide:YES afterDelay:1.0];
            
            return;
            
        }
        
        NSDictionary *params = @{@"username":self.inputPhoneNubText.text};
        [HttpTool postWithURL:@"http://61.135.176.43:6080/bigtobig/user/if_user_existed?" params:params success:^(id json) {
            
            NSString *str = json[@"count"];
            int a = [str intValue];
            NSLog(@"%@",str);
            
            if (a == 1) {
                
            MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
            HUD.labelText = @"此手机号已注册";
            HUD.mode = MBProgressHUDModeIndeterminate;
            [HUD hide:YES afterDelay:1.0];
                
                
                
            }else{
                
                NSDictionary *params = @{@"mobile":self.inputPhoneNubText.text};
                [HttpTool postWithURL:@"http://61.135.176.43:6080/bigtobig/sms/get_random_message?" params:params success:^(id json) {
                    NSLog(@"%@",json);
                    NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
                    
                    NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
                    NSLog(@"%@",dict);
                    
                    NSString *str = dict[@"random_message"];
                    
                    if ([self.inputSecurityCodeText.text isEqualToString:str]) {
                        [self.scrollView setContentOffset:CGPointMake(kScreenWidth, 0) animated:YES];
                        
                    }else{
                        
                        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
                        hud.mode = MBProgressHUDModeText;
                        hud.labelText = @"请重新输入验证码";
                        [hud hide:YES afterDelay:1];
                    }
                    
                    
                } failure:^(NSError *error) {
                    
                    if (error.code == -1004) {
                        
                        MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
                        HUD.labelText = @"网络请求失败";
                        HUD.mode = MBProgressHUDModeIndeterminate;
                        [HUD hide:YES afterDelay:1.0];
                        
                    }
                    
                }];

                
            }
            
        } failure:^(NSError *error) {
            
            
        }];
        
        NSLog(@"手机号码正确调接口发送验证码");
        

        
    }
    
}

///调用方法  设置只能输入数字  和   限制字符长度
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string{
    //身份证  只能输入数字 限制字数18位
//    textField.keyboardAppearance =  UIKeyboardTypeNumberPad;

    if(textField == self.inputSecurityCodeText){
        
//        textField
        NSUInteger lengthOfString = string.length;
        for (NSInteger loopIndex = 0; loopIndex < lengthOfString; loopIndex++) {//只允许数字输入
            unichar character = [string characterAtIndex:loopIndex];
            if (character < 48) return NO; // 48 unichar for 0
            if (character > 57) return NO; // 57 unichar for 9
        }
        NSUInteger proposedNewLength = textField.text.length - range.length + string.length;
        
            if (proposedNewLength > 4)
                
                return NO;//限制长度

    }if (self.inputPhoneNubText == textField) {
        
        NSUInteger proposedNewLength = textField.text.length - range.length + string.length;
        
        if (proposedNewLength > 11)
            
            return NO;//限制长度
    }if (self.inputSecreatText == textField) {
        NSUInteger proposedNewLength = textField.text.length - range.length + string.length;
        
        if (proposedNewLength > 18)
            
            return NO;//限制长度

    }if (self.sureSecreatText == textField) {
        NSUInteger proposedNewLength = textField.text.length - range.length + string.length;
        
        if (proposedNewLength > 18)
            
            return NO;//限制长度
        
    }


    return YES;
}

// 实现UIScrollView的滚动方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    
    // 1. 获取滚动的x方向的偏移值
    CGFloat offsetX = scrollView.contentOffset.x;
    // 用已经偏移了得值, 加上半页的宽度
    offsetX = offsetX + (kScreenWidth * 0.5);
    
    // 2. 用x方向的偏移的值除以一张图片的宽度(每一页的宽度)，取商就是当前滚动到了第几页（索引）
    NSLog(@"11111111%f",offsetX);
    NSInteger page = offsetX / kScreenWidth;
    
    // 3. 将页码设置给UIPageControl
    self.pageControl.currentPage = page;
    NSLog(@"%d",page);

}


- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{

    // 1. 获取当前的UIPageControl的页码
    NSInteger page = self.pageControl.currentPage;
    
    // 3. 用每页的宽度 * (页码 + 1) == 计算除了下一页的contentOffset.x
    CGFloat offsetX = page * self.scrollView.frame.size.width;
    NSLog(@"%f",offsetX);
    // 4. 设置UIScrollView的contentOffset等于新的偏移的值
    [self.scrollView setContentOffset:CGPointMake(offsetX, 0) animated:YES];
    
}
- (void)textFieldDidBeginEditing:(UITextField *)textField{

    
}
- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    
    //回退键盘
    return [textField endEditing:YES];
}

- (void)complate{
    

    
    if (self.inputNameNubText.text.length == 0) {
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"请输入姓名";
        [hud hide:YES afterDelay:1];

    }else if(self.agreeCircleBtn.selected == NO){
        
        MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
        hud.mode = MBProgressHUDModeText;
        hud.labelText = @"请确认遵守协议";
        [hud hide:YES afterDelay:1];

        
    }else{
    
        
        NSDictionary *parameters = @{@"username":self.inputPhoneNubText.text,@"password":self.inputSecreatText.text,@"name":self.inputNameNubText.text};
        
        [HttpTool postWithURL:@"http://61.135.176.43:6080/bigtobig/user/register?" params:parameters success:^(id json) {
            
            NSData *data = [NSJSONSerialization dataWithJSONObject:json options:NSJSONWritingPrettyPrinted error:nil];
            NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:nil];
            
            NSLog(@"%@",dict);
            
            
            NSMutableArray *registerArray = [NSMutableArray array];
            self.registerArray = registerArray;
            registerModel *registerMD = [registerModel RegisterModelWithDict:dict];
            self.registerModel = registerMD;
            [self.registerArray addObject:registerMD];
            
            NSLog(@"%@%@",self.registerArray,registerMD.name);
            NSString *returnCode = [NSString stringWithFormat:@"%@",dict[@"return_code"]];
            NSLog(@"%@",returnCode);
            
            if ([returnCode isEqualToString:@"0"]) {
                
                [UIView animateWithDuration:1.0 animations:^{
                    
                    MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
                    HUD.labelText = @"注册成功";
                    HUD.mode = MBProgressHUDModeIndeterminate;
                    [HUD hide:YES afterDelay:1.0];
                    
                } completion:^(BOOL finished) {
                    
                    [self sdkLogin];
                    //登录成功后保存下这个值判断程序启动的时候登陆过直接进入否则进入登录界面
                    
                    NSUserDefaults* isLogin = [NSUserDefaults standardUserDefaults];
                    [isLogin setBool:YES forKey:@"isOn"];
                    [isLogin synchronize];
                    

                    
                    NSUserDefaults* inputNub = [NSUserDefaults standardUserDefaults];
                    [inputNub setObject:self.inputPhoneNubText.text forKey:@"InputNub"];
                    [inputNub synchronize];
                    
                    NSUserDefaults* inputSecreate = [NSUserDefaults standardUserDefaults];
                    [inputSecreate setObject:self.inputSecreatText.text forKey:@"Secreate"];
                    [inputSecreate synchronize];
                    

                    KeyWindow.rootViewController = [MainTabBarController new];
            
                    
                }];
                
            }else{
                

                [UIView animateWithDuration:1.0 animations:^{
                    
                    MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
                    HUD.labelText = @"注册失败";
                    HUD.mode = MBProgressHUDModeIndeterminate;
                    [HUD hide:YES afterDelay:1.0];
                    

                } completion:^(BOOL finished) {
                    

                    [self.navigationController popViewControllerAnimated:YES];
                    
                    
                }];
            }

        } failure:^(NSError *error) {
            
            
                MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:KeyWindow animated:YES];
                HUD.labelText = @"网络请求失败";
                HUD.mode = MBProgressHUDModeIndeterminate;
                [HUD hide:YES afterDelay:1.0];
            
        }];
        
    }

}



- (void)sdkLogin{
    
    [AVCamViewController getLoginUserInfo:self.registerModel.primary_key withPassword:self.inputSecreatText.text withBlock:^(NSString *code, NSInteger activityId, NSInteger userId, NSString *tokenId) {
        
        //登陆成功
        if ([code isEqualToString:@"0"])
        {
            
            
            NSLog(@"%ld",activityId);
            
            NSString *str = [NSString stringWithFormat:@"%ld",activityId];
            NSUserDefaults *activityIdDefaults = [NSUserDefaults standardUserDefaults];
            [activityIdDefaults setObject:str forKey:@"activty_id"];
            [activityIdDefaults synchronize];
            
            dispatch_async(dispatch_get_main_queue(), ^{
                
                
            });
            
        }else
        {
            NSLog(@"登陆失败");
        }
    }];
    
}
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self.inputPhoneNubText endEditing:YES];
    [self.inputSecreatText endEditing:YES];
    [self.inputNameNubText endEditing:YES];
    [self.sureSecreatText endEditing:YES];
    [self.inputSecurityCodeText endEditing:YES];
}

@end
